package com.sunlg.smarthome.contract.presenter;

import com.sunlg.smarthome.base.App;
import com.sunlg.smarthome.base.C;
import com.sunlg.smarthome.contract.AddSecurityDeviceContract;
import com.sunlg.smarthome.data.entity.SecurityDevice;
import com.sunlg.smarthome.data.remote.MySubscriber;
import com.sunlg.smarthome.data.remote.Result;
import com.sunlg.smarthome.data.remote.SHServiceWrapper;
import com.sunlg.smarthome.utils.PreferHelper;

/**
 * Created by sunlg on 2017/5/25.
 */

public class AddSecurityDevicePresenter implements AddSecurityDeviceContract.Presenter {

    private PreferHelper preferHelper;
    private SHServiceWrapper mSHServiceWrapper;
    private AddSecurityDeviceContract.View mView;

    public AddSecurityDevicePresenter() {
        preferHelper = new PreferHelper(App.getInstance(), "gateway");
        mSHServiceWrapper = App.getInstance().getServiceWrapper();
    }

    @Override
    public void addSecurityDevice(SecurityDevice securityDevice) {
        securityDevice.setGatewayId(preferHelper.getInt("id", 0));
        mSHServiceWrapper.addSecurityDevice(securityDevice, new MySubscriber<Result>() {
            @Override
            public void onSuccess(Result data) {
                if(data.getCode() == C.ActionStatus.SUCCESS){
                    mView.addSuccess();
                }else {
                    _onError(new Throwable(data.getMsg()));
                }
            }

            @Override
            public void _onError(Throwable e) {
                mView.showError(e.getMessage());
            }
        });
    }

    @Override
    public void attachView(AddSecurityDeviceContract.View view) {
        mView = view;
    }

    @Override
    public void dettachView() {
        mView = null;
    }
}
